package com.monkeygroup.mygoplus.ecchat.ui.contact;

import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.ecchat.common.utils.ToastUtil;
import com.monkeygroup.mygoplus.ecchat.ui.contact.adapter.MemberListAdapter;
import com.monkeygroup.mygoplus.ecchat.ui.contact.entity.ContactMemberDTO;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/26.
 */
public class LocatingSearchActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout layout_radaring, layout_result;
    private ListView radar_result_list;
    private ImageView animation_radar_loading;
    private AnimationDrawable animationDrawable;
    private List<ContactMemberDTO> resultList = new ArrayList<>();
    private MemberListAdapter adapter;
    private final String TAG = "LocatingSearchActivity";

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_location_search);
    }

    @Override
    public void initView() {
        animation_radar_loading = (ImageView) findViewById(R.id.animation_radar_loading);
        layout_radaring = (LinearLayout) findViewById(R.id.layout_radaring);
        layout_radaring.setVisibility(View.VISIBLE);
        layout_result = (LinearLayout) findViewById(R.id.layout_result);
        layout_result.setVisibility(View.GONE);
        radar_result_list = (ListView) findViewById(R.id.radar_result_list);
    }

    @Override
    public void setListener() {
        findViewById(R.id.layout_back).setOnClickListener(this);
    }

    @Override
    public void doEvent() {
        animation_radar_loading.setImageResource(R.drawable.animation_radar_loading);
        animationDrawable = (AnimationDrawable) animation_radar_loading.getDrawable();
        animationDrawable.start();

        initAdapter();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doRadarResult();
            }
        }, 1000);
    }

    private void initAdapter() {
        adapter = new MemberListAdapter(LocatingSearchActivity.this, resultList);
        radar_result_list.setAdapter(adapter);
    }

    private void setResultData(List<Map<String, Object>> list) {
        resultList.clear();
        for (Map<String, Object> item : list) {
            ContactMemberDTO memberDTO = new ContactMemberDTO();
            if (item.get("username") != null && !item.get("username").equals("")) {
                memberDTO.setName((String) item.get("username"));
            } else if (item.get("userID") != null && !item.get("userID").equals("")) {
                memberDTO.setName((String) item.get("userID"));
            } else {
                memberDTO.setName((String) item.get("mobile"));
            }

            memberDTO.setId((String) item.get("mobile"));
            memberDTO.setHeadUrl((String) item.get("headIcon"));
            resultList.add(memberDTO);
        }

        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onStop() {
        super.onStop();
        animationDrawable.stop();
    }

    /**
     * 獲取雷達結果列表
     **/
    private void doRadarResult() {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", MyGoApplication.myECUserid);

        String requestUrl = RequestUrl.baseUrl + RequestUrl.requestRaderList;
        HttpModel httpModel = new HttpModel(requestUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    animationDrawable.stop();

                    layout_result.setVisibility(View.VISIBLE);
                    layout_radaring.setVisibility(View.GONE);

                    Map<String, Object> map = (Map) data;
                    List<Map<String, Object>> list = (ArrayList) map.get("list");
                    setResultData(list);
                } else {
                    ToastUtil.showMessage(resultMsg);
                }
            }
        }, true);

        httpModel.startRequest();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_back:
                finish();
                break;
            default:
                break;
        }
    }
}
